<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */
if (!defined('IN_DOUCO_API')) {
    die('Access Denied');
}


function doPageList(){

    global $_GPC, $_W,$dou;
    $pindex = max(1, intval($_GPC["page"]));
    $cat_id =  intval($_GPC["cid"]);
    $psize = 10;

    $data['this'] = get_cate_info('product_category',$cat_id);
    if(!$data['this']){
        $data['this']['cat_name'] = '产品中心';
    }
    // 如果为末级分类则显示当前同级分类
    if ($dou->dou_child_id('product_category', $cat_id)) {
        $parent_id = $cat_id;
    } else {
        $parent_id = $dou->get_one("SELECT parent_id FROM " . $dou->table('product_category') . " WHERE cat_id = '$cat_id'");;
    }

    if($cat_id){
        $where = ' WHERE cat_id IN (' . $cat_id . $dou->dou_child_id('product_category', $cat_id) . ')';
    }else{
        $where = '';
    }
    $data['cate'] = $dou->get_category('product_category', $parent_id, $cat_id);

    $cate_first['cat_name'] = '全部';
    $cate_first['cat_id'] = $parent_id;
    array_unshift($data['cate'], $cate_first);

    $data['num'] = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename("product") . $where);
    $data['page_size'] = $psize;
    $data["list"] = pdo_fetchall("SELECT * FROM " . tablename("product") .  $where . " ORDER BY sort DESC,id DESC LIMIT " . ($pindex - 1) * $psize . "," . $psize);

    foreach ($data["list"] as $key=>$value){
        $data['list'][$key]['add_time'] = date("Y-m-d", $value['add_time']);
        $data['list'][$key]['image'] = ROOT_URL.$value['image'];
        $data['list'][$key]['description'] = $value['description'] ? $value['description']: $dou->dou_substr($value['content'], 150, false);;
    }
    return wxapp_result(0,'success',$data);

}


/**
 * 获取产品信息
 */
function doPageGet(){
    global $_GPC, $_W,$dou,$_MODULE;
    $id = intval($_GPC["id"]);
    $product = get_item('product',$id);
    if(!$product){
        return wxapp_result(404,'请求错误，文件不存在！');
    }


    $product['image'] = ROOT_URL.$product['image'];

    if($product['defined']){
        // 格式化自定义参数
        foreach (explode(',', $product['defined']) as $row) {
            $row = explode('：', str_replace(":", "：", $row));

            if ($row['1']) {
                $defined[] = array (
                    "arr" => $row['0'],
                    "value" => $row['1']
                );
            }
        }

        $product['defined'] = $defined;
    }
    //产品售后保障
    $product['service'] = get_item('page',$_W['setting']['wxapp_service_id']);
    //是否开启购买
    $product['is_sale'] = $_MODULE['open']['order']?1:0;

    //检测是否收藏本产品
    $user = get_user_by_openid(wxapp_getopenid());
    $collect = pdo_fetch("SELECT count(*) as n FROM " . tablename("page_collect") . " WHERE user_id = :user_id AND page_type = :page_type AND page_id = :page_id", array(":user_id"=>$user['user_id'],":page_type" => "product", ":page_id" => $id));
    //是否收藏本产品
    $product['collect'] = $collect["n"] > 0 ? 1:0;


    return wxapp_result(0,'success',$product);

}